package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import transfer.StringData;
import client.Client;

public class CreateRoomView extends ChessOnlineView{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8197999159511025333L;
	private JScrollPane listScroller;
	DefaultListModel<String> listModel;
	JList<String> list;
	public CreateRoomView(Client mainScreen) {
		super(mainScreen);

		listModel = new DefaultListModel<String>();
/*		listModel.addElement("sinbad1");
		listModel.addElement("sinbad2");
		listModel.addElement("sinbad3");*/
		list = new JList<String>(listModel);
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL_WRAP);
		listScroller = new JScrollPane(list);
		listScroller.setBounds(10,50,300,100);
		this.setLayout(null);
		this.add(listScroller);
		
		JButton btnNewRoom = new JButton("New room");
		
		btnNewRoom.setBounds(10, 179, 108, 23);
		this.add(btnNewRoom);
		
		btnNewRoom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
//				listModel.addElement("new room");
				JPanel myPanel = new JPanel();
			      myPanel.add(new JLabel("Name : "));
			      JTextField nameField = new JTextField(20);
			      myPanel.add(nameField);
			      int result = JOptionPane.showConfirmDialog(null, myPanel, "Enter name 's room",JOptionPane.OK_CANCEL_OPTION);
			      
			      if (result == JOptionPane.OK_OPTION) {
			    	  System.out.println(nameField.getText());
			    	  String name = nameField.getText();
			    	  listModel.addElement(nameField.getText());
			    	  mMainScreen.getCw().sendObject(new StringData("New room", name));
			      }
			      
		
				//System.out.println(list.getSelectedValue());
			}
		});
		
		
	}
	public void updateRoom(String name) {
		listModel.addElement(name);
		
	}
	

}
